import { WeaTableNew, WeaForm } from 'comsMobx'
const { TableStore } = WeaTableNew;
import { WeaTools, WeaLocaleProvider } from "ecCom"
const getLabel = WeaLocaleProvider.getLabel;
import * as  Apis from '../../../apis/project';
import * as Common_Apis from '../../../apis/index';
import { TaskInfoStore } from '../.././task/taskInfoStore'
import { ExchangeStore } from "../.././exchangeStore"
import { RelateListStore } from '../.././taskListStore';
import { observable, action, autorun, toJS } from 'mobx';
import { ShareStore } from '../.././shareStore'

export class TaskListStore {

    @observable prjid = "";
    @observable loading = false;
    @observable taskListform = new WeaForm();
	@observable taskListquickform = new WeaForm();
	@observable taskListTabs = [];
	@observable taskListSearchParams = {
		version: "0"
    };
    @observable showSearchAd = false  // 高级搜索显示
	@observable rightMenu = [];
	@observable taskListQuickitems = [];
	@observable taskListRight = {
		hasRight: false,
		verified: false
	}
	@observable exchangeStore = new ExchangeStore();
	@observable showEditTask = false;
	@observable shareStore = new ShareStore();
	@observable relateListStore = new RelateListStore();
    @observable taskInfoStore = new TaskInfoStore();
    @observable taskListTableStore = new TableStore({
		dataHandle: (datas) => {  //任务列表
			datas.forEach((data) => {
				if (typeof (data.subListspan) == 'string') {
					data.subListspan = data.subListspan ? JSON.parse(data.subListspan) : undefined;
				}
			});
			return datas;
		}
	});
    
    //任务列表
	getPrjTaskList = (params = {}) => {
		this.loading = true;
		let newParams = { ...toJS(this.taskListSearchParams), ...toJS(this.taskListform.getFormParams()), ...toJS(this.taskListquickform.getFormParams()), prjid: this.prjid, ...params };
		for (let item in newParams) {
			if (newParams[item] == "") {
				delete newParams[item]
			}
		}
		if (newParams.version == "0") { newParams.version = "" }
		Apis.getPrjTaskList(newParams).then(data => {
			if (!data.isright && typeof (data.isright) !== "undefined") {
				this.taskListRight = {
					hasRight: data.isright,
					verified: true
				};
			} else {
				this.taskListRight = {
					hasRight: true,
					verified: true
				};
				this.taskListTableStore.getDatas(data.sessionkey, 1);
				this.rightMenu = data.rightMenus;
				this.taskListSearchParams = params.version==""?{version:"0"}:{...this.taskListSearchParams,...params};			
			};
			this.loading = false;
		})
	}
	//任务列表-condition
	getPrjTaskCondition = () => {
		this.taskListform = new WeaForm();
		this.taskListquickform = new WeaForm();
		this.condition = [];
		this.taskListQuickitems = [];
		Common_Apis.getTaskCondition({ conditiontype: "prjtasklist", prjid: this.prjid }).then(data => {
			this.condition = data.condition;
			this.taskListQuickitems = data.quickitmes;
			this.taskListform && !this.taskListform.isFormInit && this.taskListform.initFormFields(data.condition);
			this.taskListquickform && !this.taskListquickform.isFormInit && this.taskListquickform.initFormFields(data.quickitmes);
		})
	}
	getPrjTaskVersion = () => {
		Apis.getPrjTaskVersion({ prjid: this.prjid }).then(data => {
			this.taskListTabs = data;
		})
    }
    
    initData = (prjid) => {
        this.prjid =  prjid;
        this.getPrjTaskList();
		this.getPrjTaskCondition();
		this.getPrjTaskVersion();
    }

    onShowColumn = () => {   //显示定制列
		if (this.selectTabKey == "3") {
			this.prjSubStore.setColSetVisible(true);
			this.prjSubStore.tableColSet(true)
		}
	}

    //高级搜索显隐
	setShowSearchAd = (bool) => {
		this.showSearchAd = bool
	}

    //高级搜索设值
	setFormFields = (value) => {
        this.taskListform.updateFields(value, false);    //true代表完全覆盖方式更新条件值
    }
    
    //form 重置
	clearFormFields = () => {
        this.taskListform.reset();      //清除查询条件值
	}

}

